const jwt = require('jwt-simple');

const jwtEncode = (adminName, secret) => {
    return jwt.encode({
        adminName,
        exp: Date.now() / 1000 + 60 * 60 * 24 //秒的时间戳
    }, secret);
}

const jwtDecode = (token, secret) => {
    try {
        const {
            adminName
        } = jwt.decode(token, secret);
        //如果解码成功,则返回一个对象 对象的code状态是1,并还有一个用户名
        return {
            code: 1,
            adminName
        }

    } catch (e) {
        console.log("解码失败",e.message);
        //如果解码失败,则返回一个对象,状态是0
        return {
            code: 0
        }
    }
}


module.exports = {
    jwtEncode,
    jwtDecode
}